﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using mscook.mvc.Core;
using System.Linq.Expressions;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class ResourceTypeController : BaseController
    {
        //
        // GET: /weblogin/ResourceType/

        private IResourceOfTypeRepository iResourceOfTypeRepository;
        private IResourceTypeRepository iResourceTypeRepository;
        private ILoginUserService iLoginUserService;
        public ResourceTypeController(IResourceOfTypeRepository iResourceOfTypeRepository, IResourceTypeRepository iResourceTypeRepository, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iResourceOfTypeRepository = iResourceOfTypeRepository;
            this.iResourceTypeRepository = iResourceTypeRepository;
            this.iLoginUserService = iLoginUserService;
        }

        public ActionResult Index(string nowPageNum, string per = "", string name = "", string resourceCategory="")
        {
            Expression<Func<ResourceType, bool>> select_expression = o => (string.IsNullOrEmpty(name) || o.Name.Contains(name))
                &&(string.IsNullOrEmpty(resourceCategory)||o.ResourceCategory.ToString().Equals(resourceCategory))
                ;

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<ResourceType> resourceTypeList = iResourceTypeRepository.GetParentResourceTypeByLimit("", pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();

            ViewData["name"] = name;
            ViewData["resourceCategory"] = resourceCategory;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(resourceTypeList);
        }

        public ActionResult ChildType(string id)
        {
            IList<ResourceType> resourceTypeList = iResourceTypeRepository.GetResourceByParent(id);
            ViewData["parentId"] = id;
            return View(resourceTypeList);
        }



        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult add(string parentId)
        {

            ResourceType parentResource = iResourceTypeRepository.Get(parentId);
            if (parentResource != null)
            {
                ViewData["parentId"] = parentResource.Id;
                ViewData["parentName"] = parentResource.Name;
                ViewData["resourceCategory"] = parentResource.ResourceCategory;


            }


            InitNavBar();
            return View();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult add(ResourceType resourceType, string ParentTypeId,bool toParent=false)
        {
            ResourceType parentResource = iResourceTypeRepository.Get(ParentTypeId);
            if (ModelState.IsValid)
            {
                if (parentResource != null)
                {
                    resourceType.ParentType = parentResource;
                    resourceType.ResourceCategory = parentResource.ResourceCategory;
                }
                
                iResourceTypeRepository.SaveOrUpdate(resourceType);

            }
            return toParent ? RedirectToAction("ChildType", new { id = ParentTypeId }) : RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
            ResourceType resourceType = iResourceTypeRepository.Get(id);
            if (resourceType != null)
            {
                InitNavBar();
                return View(resourceType);
            }
            InitNavBar();
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string Id, string ParentTypeId)
        {
            ResourceType resourceType = iResourceTypeRepository.Get(Id);
            ResourceType parentResource = iResourceTypeRepository.Get(ParentTypeId);
            if (ModelState.IsValid)
            {

                UpdateModel(resourceType);
                resourceType.ParentType = parentResource;

                iResourceTypeRepository.SaveOrUpdate(resourceType);

                return RedirectToIndex();
            }
            return View();
        }
        public ActionResult ResourceTypeModal(string nameInput, string idInput, string type, string linkBtn = "", string num = "")
        {
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(num, "10");
            long total = 0;
            IList<ResourceType> resourceTypeList = iResourceTypeRepository.GetAllParentResourceType(type);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nameInput"] = nameInput;
            ViewData["idInput"] = idInput;
            ViewData["linkBtn"] = linkBtn;

            return View(resourceTypeList);
        }

        public ActionResult MultiResourceTypeModal(string nameInput, string idInput,string type="", string linkBtn = "", string num = "")
        {
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(num, "10");
            long total = 0;
            //IList<ResourceType> resourceTypeList = iResourceTypeRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total);
            IList<ResourceType>  resourceTypeList = iResourceTypeRepository.GetChildTypesByLimit(type, pageHelper.Start, pageHelper.PerValue, out total);
          
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nameInput"] = nameInput;
            ViewData["idInput"] = idInput;
            ViewData["linkBtn"] = linkBtn;

            return View(resourceTypeList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(string id)
        {
            iResourceTypeRepository.Delete(id);
            return RedirectToIndex();
        }


    }
}
